import http from "@/api";

// 工位相关接口
export namespace Workstation {
  // 创建工位请求参数
  export interface CreateWorkstationRequestDto {
    workstationCode: string;
    workstationName: string;
    productionLineId: string; // 改为string类型避免精度丢失
    workstationType: string;
    status?: string;
    capacity?: number;
    position?: number;
    supportedProcessTypes?: string;
    description?: string;
  }

  // 更新工位请求参数
  export interface UpdateWorkstationRequestDto {
    workstationName: string;
    productionLineId: string; // 改为string类型避免精度丢失
    workstationType: string;
    status?: string;
    capacity?: number;
    position?: number;
    supportedProcessTypes?: string;
    description?: string;
  }

  // 工位列表项
  export interface WorkstationListItem {
    id: string;
    workstationCode: string;
    workstationName: string;
    productionLineId: string; // 改为string类型避免精度丢失
    productionLineName: string;
    workshopId: string; // 改为string类型避免精度丢失
    workshopName: string;
    factoryId: string; // 改为string类型避免精度丢失
    factoryName: string;
    workstationType: string;
    status: string;
    capacity?: number;
    position?: number;
    location: string;
    supportedProcessTypes?: string;
    description?: string;
    createTime: string;
    updateTime: string;
  }

  // 工位列表响应
  export interface WorkstationListResponse {
    records: WorkstationListItem[];
    total: number;
  }
}

/** 创建工位 */
export const createWorkstation = (params: Workstation.CreateWorkstationRequestDto) => {
  return http.post<void>("/workstation/create", params);
};

/** 获取工位列表 */
export const getWorkstationList = (params?: {
  workstationCode?: string;
  workstationName?: string;
  productionLineId?: string; // 改为string类型避免精度丢失
  workshopId?: string; // 改为string类型避免精度丢失
  factoryId?: string; // 改为string类型避免精度丢失
  workstationType?: string;
  status?: string;
  current?: number;
  size?: number;
}) => {
  return http.get<Workstation.WorkstationListResponse>("/workstation/list", params);
};

/** 更新工位 */
export const updateWorkstation = (id: string, params: Workstation.UpdateWorkstationRequestDto) => {
  return http.put<void>(`/workstation/${id}`, params);
};

/** 删除工位 */
export const deleteWorkstation = (id: string) => {
  return http.delete<void>(`/workstation/${id}`);
};

/** 批量删除工位 */
export const batchDeleteWorkstation = (ids: string[]) => {
  return http.delete<void>("/workstation/batch", { data: { ids } });
};

/** 获取工位详情 */
export const getWorkstationById = (id: string) => {
  return http.get<Workstation.WorkstationListItem>(`/workstation/${id}`);
};

/** 根据产线ID获取工位列表 */
export const getWorkstationsByProductionLineId = (productionLineId: string) => {
  return http.get<Workstation.WorkstationListItem[]>(`/workstation/productionLine/${productionLineId}`);
};

/** 根据产线ID获取启用状态的工位列表 */
export const getEnabledWorkstationsByProductionLineId = (productionLineId: string) => {
  return http.get<Workstation.WorkstationListItem[]>(`/workstation/productionLine/${productionLineId}/enabled`);
};

/** 获取启用状态的产线列表 */
export const getEnabledProductionLineList = () => {
  return http.get<{ id: string; lineName: string; factoryName: string; workshopName: string }[]>("/productionLine/enabled");
};